package com.example.testapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

/**
 * @author wwq on 2019/7/17  11:15.
 */
public class ListViewAdapter extends BaseAdapter {

    private List<Question> question;
    private LayoutInflater inflater;

    ListViewAdapter(List<Question> question, Context context) {
        this.question = question;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return question == null ? 0 : question.size();
    }

    @Override
    public Object getItem(int i) {
        return question.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;

        if (view == null) {
            view = inflater.inflate(R.layout.question_item, null);
            holder = new ViewHolder();
            holder.title = view.findViewById(R.id.title);
            holder.btn = view.findViewById(R.id.btn);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Question question = (Question) getItem(i);
        holder.title.setText(question.getTitle());
        //三目运算符
        holder.btn.setVisibility(i == 1 ? View.GONE : View.VISIBLE);

        return view;
    }

    static class ViewHolder {
        TextView title;
        Button btn;
    }
}
